package com.wowotou.rabbitmq.config;

import org.springframework.amqp.core.*;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

@Configuration
public class TtlQueueConfig {
    // 普通交换机
    private static final String NORMAL_EXCHANGE = "X";
    // 死信交换机
    private static final String DEAD_EXCHANGE = "Y";
    // 三个队列
    private static final String QUEUE_A = "QA";
    private static final String QUEUE_B = "QB";
    private static final String QUEUE_C = "QC";
    private static final String QUEUE_D = "QD";

    @Bean("xExchange")
    public DirectExchange xExchange(){
        return new DirectExchange(NORMAL_EXCHANGE,true,false);
    }

    @Bean("yExchange")
    public DirectExchange yExchange(){
        return new DirectExchange(DEAD_EXCHANGE,true,false);
    }

    @Bean("queueA")
    public Queue queueA(){
        Map<String, Object> args = new HashMap<>(3);
        //声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_EXCHANGE);
        //声明当前队列的死信路由 key
        args.put("x-dead-letter-routing-key", "YD");
        //声明队列的 TTL
        args.put("x-message-ttl", 10000);
        return QueueBuilder
                       //持久化
                       .durable(QUEUE_A)
                       .withArguments(args)
                       .build();
    }

    @Bean("queueB")
    public Queue queueB(){
        Map<String, Object> args = new HashMap<>(3);
        //声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_EXCHANGE);
        //声明当前队列的死信路由 key
        args.put("x-dead-letter-routing-key", "YD");
        //声明队列的 TTL
        args.put("x-message-ttl", 40000);

        return QueueBuilder
                       //持久化
                       .durable(QUEUE_B)
                       .withArguments(args)
                       .build();
    }

    @Bean("queueC")
    public Queue queueC(){
        Map<String, Object> args = new HashMap<>(2);
        //声明当前队列绑定的死信交换机
        args.put("x-dead-letter-exchange", DEAD_EXCHANGE);
        //声明当前队列的死信路由 key
        args.put("x-dead-letter-routing-key", "YD");
        return QueueBuilder
                       //持久化
                       .durable(QUEUE_C)
                       .withArguments(args)
                       .build();
    }

    @Bean("queueD")
    public Queue queueD(){
        return QueueBuilder
                       //持久化
                       .durable(QUEUE_D)
                       .build();
    }

    // 队列与交换机进行绑定
    @Bean("queueaBindingX")
    public Binding queueaBindingX(@Qualifier("queueA") Queue queue,@Qualifier("xExchange")DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("XA");
    }

    @Bean("queuebBindingX")
    public Binding queuebBindingX(@Qualifier("queueB") Queue queue,@Qualifier("xExchange")DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("XB");
    }

    @Bean("queuecBindingX")
    public Binding queuecBindingX(@Qualifier("queueC") Queue queue,@Qualifier("xExchange")DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("XC");
    }

    @Bean("queuedBindingY")
    public Binding queuedBindingY(@Qualifier("queueD") Queue queue,@Qualifier("yExchange")DirectExchange exchange){
        return BindingBuilder.bind(queue).to(exchange).with("YD");
    }
}
